const mongoose = require("./db");
const { getId } = require("../utils/getMethods");

// 活动规则
const activityRuleSchema = new mongoose.Schema({
  id: {
    type: Number,
    default: () => getId,
  },
  ruleDesc: {
    //规则描述
    type: String, // 100 减 10
    required: true,
  },
});

// 商品种类 列表
const categoryListSchema = new mongoose.Schema({
  id: {
    // 商品种类id
    type: Number,
    unique: true,
    required: true,
    default: () => getId,
  },
  name: {
    // 商品种类名称
    type: String,
    required: true,
  },
  imgUrl: {
    // 商品种类图片
    type: String,
    required: true,
  },
  parentId: {
    // 父级id
    type: Number, // 1、2、3....
    required: true,
  },
  status: {
    // 商品种类状态
    type: Number, // 1
    required: true,
  },
});

// 图片列表
const imagesListSchema = new mongoose.Schema({
  id: {
    // 图片id
    type: Number,
    default: () => getId,
  },
  skuId: {
    // 商品id
    type: Number, //	1
  },
  imgName: {
    // 图片名称
    type: String,
  },
  imgUrl: {
    // 图片地址
    type: String,
  },
});

// 商品规格属性值列表
const skuAttrValueListSchema = new mongoose.Schema({
  id: {
    type: Number,
    default: () => getId,
  },
  skuId: {
    type: Number,
  },
  attrId: {
    type: Number,
  },
  attrName: {
    type: String,
  },
  attrValue: {
    type: String,
  },
});

// 库存商品列表
const skuProductListSchema = new mongoose.Schema({
  id: {
    // 商品id
    type: Number,
    unique: true,
    required: true,
    default: () => getId,
  },
  keyword: {
    // 关键字
    type: String, // 瓜果
    required: true,
  },
  skuType: {
    // 商品类型
    type: Number, // 0,1
    required: true,
    default: 1,
  },
  isNewPerson: {
    // 是否新人专享
    type: Number, // 1，2
    default: 0,
  },
  isChecked: {
    type: Number,
    default: 0,
  },
  categoryId: {
    // 商品种类id
    type: Number, // 8.010502660600683e+37
    required: true,
    default: () => getId,
  },
  categoryName: {
    // 商品种类名称
    type: String, // categor2   categor3
  },
  imgUrl: {
    // 商品图片
    type: String,
    required: true,
  },
  isChecked: {
    // 状态
    type: Number,
    default: 0,
  },
  title: {
    // 商品的种类展示，用于在首页进行显示，进度条展示的部分
    type: String, // 水果、蔬菜、粮油、
    required: true,
  },
  price: {
    // 商品价格
    type: Number,
    required: true,
  },
  perLimit: {
    // 限购数量
    type: Number,
    required: true,
  },
  sale: {
    // 是否上架
    type: Number, // 0,1
    required: true,
  },
  ruleList: Array, // 活动规则列表
  marketPrice: Number, // 市场价
  skuName: String, // 商品名称,
  stock:Number //库存数量
});

// 购物车列表
const cartListSchema = new mongoose.Schema({
  skuId: {
    // 商品id
    type: mongoose.Types.ObjectId,
    ref: "skuProductList",
  },
  skuNum: {
    // 商品数量
    type: Number,
    required: true,
    default: 0,
  },
});

const cartListModel = mongoose.model("cartList", cartListSchema, "cartList");

const activityRuleListModel = mongoose.model(
  "activityRuleList",
  activityRuleSchema,
  "activityRuleList"
);
const skuProductListModel = mongoose.model(
  "skuProductList",
  skuProductListSchema,
  "skuProductList"
);
const categoryListModel = mongoose.model(
  "categoryList",
  categoryListSchema,
  "categoryList"
);
const imagesListModel = mongoose.model(
  "imagesList",
  imagesListSchema,
  "imagesList"
);
const skuAttrValueListModel = mongoose.model(
  "skuAttrValueList",
  skuAttrValueListSchema,
  "skuAttrValueList"
); 

module.exports = {
  cartListModel,
  activityRuleListModel,
  categoryListModel,
  skuProductListModel,
  imagesListModel,
  skuAttrValueListModel,
};
